package com.llap.exp190929.service;

import com.llap.exp190929.entity.BookType;
import com.llap.exp190929.repository.BookTypeRepository;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@AllArgsConstructor
@Slf4j
public class BookTypeService {
    private BookTypeRepository booktypeRepository;

    public BookType findBookType(int id) {
        return booktypeRepository.findById(id).orElse(null);
    }

    public Page<BookType> findBookTypes(int page, int pageSize) {
        return booktypeRepository.findAll(PageRequest.of(page - 1, pageSize));
    }

    @Transactional
    public BookType addBookType(BookType booktype) {
        if (booktype.getId() != null) return null;
        return booktypeRepository.save(booktype);
    }

    @Transactional
    public BookType updateBookType(BookType booktype) {
        if (booktype.getId() == null) return null;
        return booktypeRepository.save(booktype);
    }

    @Transactional
    public void deleteBookType(int id) {
        booktypeRepository.deleteById(id);
    }
}
